###########################################################################################################
## Payson (2021) When Cities Lobby Replication Code
## Chapter Six Appendix Table A6.1 - A6.2
## Created with R version 4.0.3 (2020-10-10) -- "Bunny-Wunnies Freak Out"
###########################################################################################################


## Load packages
library(ggplot2); library(effects); library(stargazer); 
library(tidyverse); library(reshape); library(DataCombine)


## Set working directory
setwd()



###########################################################################################################
## Table A6.1 Effect of Lobbying on Share of State Transfers to High-Income Cities
###########################################################################################################

dta <- read.csv("state_transfers.csv")

dta$year <- factor(dta$year)

m1 <- lm(prop.high ~ year + state + log.exp + house.dem.prop + gov.party + prop.lobby, data = dta)

m2 <- lm(prop.high ~ year + state + log.exp + house.dem.prop + gov.party + prop.lobby, data = dta[dta$num.city > 14,])


stargazer(m1, m2, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05),
          title = c("\\textbf{Effect of Lobbying on Share of State Transfers to High-Income Cities.} 
                    Notes here."),
          dep.var.caption = "Share Transfers to High Income Cities", 
          dep.var.labels.include = FALSE, keep=c("prop.lobby"),
          covariate.labels = c("Proportion Cities Lobbying"), 
          add.lines = list(c("State FEs", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes"),
                           c("States with > 15 Cities", "", "Yes"),
                           c("Observations", dim(m1$model)[1], dim(m2$model)),
                           c("Mean Share to High-Income Cities",
                             round(mean(m1$model$prop.high),2),
                             round(mean(m2$model$prop.high),2))))



###########################################################################################################
## Table A6.2 Effect of Lobbying on Share of State Transfers to High-Income Cities: Leads and Lags
###########################################################################################################


dta <- read.csv("state_transfers.csv")

dta$year <- factor(dta$year)


m1 <- lm(prop.high ~ year + state + prop.lobby.lag1 + prop.lobby + prop.lobby.lead1, data = dta)

m2 <- lm(prop.high ~ year + state + prop.lobby.lag1 + prop.lobby + prop.lobby.lead1, data = dta[dta$num.city > 13,])

stargazer(m1, m2, omit.table.layout = "sn", type = "text", star.cutoffs = c(0.05), 
          column.labels = c("All States", "States With > 15 Cities"), 
          title = c("Effect of Lobbying on Share of State Transfers to High-Income Cities."),
          dep.var.caption = "Share Transfers to High Income Cities, t", 
          dep.var.labels.include = FALSE, keep=c("prop.lobby"),
          covariate.labels = c("Proportion Cities Lobbying, t-1", "Proportion Cities Lobbying, t", "Proportion Cities Lobbying, t+1"), 
          add.lines = list(c("State FEs", "Yes", "Yes"),
                           c("Year FEs", "Yes", "Yes"),
                           c("Observations", dim(m1$model)[1], dim(m2$model)),
                           c("Mean Share to High-Income Cities",
                             round(mean(m1$model$prop.high),2),
                             round(mean(m2$model$prop.high),2))))

